﻿using System;

public class TimerInfo : BaseInfo
{
    /// <summary>
    /// 开始时间
    /// </summary>
    private float _startTime;

    /// <summary>
    /// 结束时间
    /// </summary>
    private float _endTime;

    /// <summary>
    /// 结束回调
    /// </summary>
    private Action _endCb;

    /// <summary>
    /// 当前时间
    /// </summary>
    private float _curTime;

    /// <summary>
    /// 差值时间
    /// </summary>
    private float _diffTime;

    /// <summary>
    /// Udpate回调
    /// </summary>
    private Action<float, float> _updateCb;

    /// <summary>
    /// 是否可以被移除
    /// </summary>
    private bool _canRemove;

    public override bool CanRemove => _canRemove;

    public TimerInfo(float delayTime, float startTime, Action endCb)
    {
        _startTime = startTime;
        _endTime = startTime + delayTime;
        _diffTime = _endTime - _startTime;
        _endCb = endCb;
        _updateCb = null;
    }

    public TimerInfo(float delayTime, float startTime, Action endCb, Action<float, float> updateCb)
    {
        _startTime = startTime;
        _endTime = startTime + delayTime;
        _diffTime = _endTime - _startTime;
        _endCb = endCb;
        _updateCb = updateCb;
    }

    public override void UpdateTime(float curTime)
    {
        _curTime = curTime;
    }

    public override bool CanInvoke()
    {
        return _curTime >= _endTime;
    }

    public override void Invoke()
    {
        _endCb?.Invoke();
        _canRemove = true;
    }

    public override void InvokeUpdate()
    {
        float elapsedTime = _curTime - _startTime;
        _updateCb?.Invoke(elapsedTime, _diffTime);
    }
}
